Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Confusion with the "extends" keyword in Typescript

Think of the following line of code in Typescript:

let x: 'a' | 'b' extends 'a' ? true : false;

I was wondering that the type of x would be true since intuitively 'a' | 'b' is an extended version of 'a' (at least my intuition says so). I thought extends would work like subsets in math. A extends B iff B ⊆ A.

However, it seems that the actual type x is false here. I guess I don't understand how exactly the extends keyword works.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Per the Liskov Subsitution Principle, if "Y extends X" or equivalently "Y is a subtype of X", then a value conforming to type Y can be used wherever a value conforming to type X is requested. This leads to a counterintuitive conclusion: when referring to the universe of possible values, if Y extends X, Y is more constrained than X. All Ys are Xs, but not all Xs are Ys.

In your example, because 'a' | 'b' could be either 'a' or 'b', that union type does not extend type 'a', because 'b' wouldn't substitute for 'a'. Instead, 'a' extends ('a' | 'b'), because all values that match 'a' would work where 'a' | 'b' is requested.

As such, A extends B iff A ⊆ B.

One reason this is less intuitive in TypeScript is that your example deals in unions of literal values. It might make more sense for us to think of this in terms of objects, where {foo: number, bar: number} extends {foo: number}. The latter, {foo: number}, could have a bar property of any type or no bar at all. The former {foo: number, bar: number} is more specific and more constrained: not only is foo a number, but bar is also a number. This also matches the use of extends in class or interface definitions: The subclass or subinterface adds properties and methods, which further constrains instances compared to the superclass or superinterface.

This is also why never is assignable to everything: never is the most constrained type because no actual values match it, so never extends everything. The empty set is a subset of every set; the empty type never is the subtype of every type and can be assigned to every other type.

type Foo = { foo: number };
type Bar = never extends Foo ? true : false;  // true
about 4 years ago · Juan Pablo Isaza Relatório

0

Extends checks type of the instance, doesn't checks the possible variables. Even if it works it would make no sense for complicated types like classes. If you check that with String it would be true.

let x: ('a' | 'b') extends String ? true : false;

Output would be;

true

You can take look at that documentation about Conditional Types from https://www.typescriptlang.org/docs/handbook/2/conditional-types.html

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda